package com.ambimmort.shj.service.generalfile;

import cn.net.greenet.common.db_service.BaseDao;
import cn.net.greenet.common.db_service.ConfigManager;
import cn.net.greenet.common.policy_service.PolicySupport;
import cn.net.greenet.db.HibernateUtil;
import cn.net.greenet.db.bean.Appdef;
import cn.net.greenet.db.bean.AppdefId;
import cn.net.greenet.generalfile.logic.DataChange;
import cn.net.greenet.generalfile.logic.GeneralFileProcess;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.springframework.beans.factory.annotation.Autowired;

import java.io.PrintWriter;
import java.sql.ResultSet;
import java.util.List;

/**
 * Created by 师梁 on 2015/7/30.
 */
public class GetApp2DBService {
//    @Autowired
//    private SessionFactory hibernateSessionFactory;
    ResultSet rs = null;
    BaseDao baseDao = null;
    String sql = null;

    private void init() {
        baseDao = new BaseDao();
    }

    private void close() {
        if (baseDao != null) {
            baseDao.close();
        }
    }

    public boolean get(String path,String MessageType,String policy_instance,GeneralFileProcess pro ){

        boolean result = false;
        if (path != null) {
            List<String> list = pro.getContent(path, 3);
            if (!list.isEmpty()) {
                init();
                try {
                    Appdef appdef = null;
                    AppdefId appdefid = null;
                    for (String line : list) {
                        appdef = new Appdef();
                        appdefid = new AppdefId();
                        String[] ss = line.split("\t");
                        String AppName = ss[0];
                        String AppType = ss[1];
                        String AppID = ss[2];
                        int id = DataChange.hex2dec(AppType);
                        ss[1] = id + "";
                        int appid = DataChange.hex2dec(AppID);
                        ss[2] = appid + "";
                        Object[] param = {AppName,id, appdef};
                        sql = "insert into appdef (Name, AppType_id,id ) values (?, ?, ?)";
                        baseDao.executeUpdate(sql, param);
                    }
                    result = true;
                } catch (Exception e) {
                    result = false;
                } finally {
                    close();
                }
                try {
                    PolicySupport policySupport = PolicySupport.getInstance();
                    String policy = policySupport.getApi().getMessage("0xC9", "默认库", 0).getSvnFile().getContent();
                    String comment = policySupport.getApi().getMessage("0xC9", "默认库", 0).getSvnFile().getComment();
                    policySupport.getApi().updatePolicy("0xC9", "默认库", 0, policy, comment);
                } catch (Exception e) {
                }
            }
        }
        return result;
    }

////        GeneralFileProcess pro = new GeneralFileProcess(path, MessageType, policy_instance);
////        String name = ConfigManager.getInstance().getString("appFileName");
////        path = pro.saveFile(request, name);
//        boolean result = false;
//        if (path != null) {
//            List<String> list = pro.getContent(path, 3);
//            if (!list.isEmpty()) {
//                Session session = hibernateSessionFactory.openSession();
//                session.clear();
//                Transaction tx = session.beginTransaction();
//                try {
//                    Appdef appdef = null;
//                    AppdefId appdefid = null;
//                    for (String line : list) {
//                        appdef = new Appdef();
//                        appdefid = new AppdefId();
//                        String[] ss = line.split("\t");
//                        String AppName = ss[0];
//                        String AppType = ss[1];
//                        String AppID = ss[2];
//                        int id = DataChange.hex2dec(AppType);
//                        ss[1] = id + "";
//                        int appid = DataChange.hex2dec(AppID);
//                        ss[2] = appid + "";
//                        appdefid.setName(AppName);
//                        appdefid.setAppTypeId(id);
//                        appdef.setId(appdefid);
//                        session.save(appdef);
//                    }
//                    tx.commit();
//                    result = true;
//                } catch (Exception e) {
//                    tx.rollback();
//                    result = false;
//                } finally {
//                    session.close();
//                }
//                try {
//                    PolicySupport policySupport = PolicySupport.getInstance();
//                    String policy = policySupport.getApi().getMessage("0xC9", "默认库", 0).getSvnFile().getContent();
//                    String comment = policySupport.getApi().getMessage("0xC9", "默认库", 0).getSvnFile().getComment();
//                    policySupport.getApi().updatePolicy("0xC9", "默认库", 0, policy, comment);
//                } catch (Exception e) {
//                }
//            }
//        }
//        return result;
//    }
}
